package com.example.expandprogresslist;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<ExpandDataEntity> dataEntityList;


    public ExpandableListViewAdapter(Context context, List<ExpandDataEntity> data) {
        this.context = context;
        this.dataEntityList = data;
    }

    @Override
    public int getGroupCount() {
        return dataEntityList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataEntityList.get(groupPosition).getData().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return dataEntityList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataEntityList.get(groupPosition).getData().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getCombinedChildId(groupPosition, childPosition);
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_header, parent, false);
            viewHolder = new GroupViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }
        ExpandDataEntity itemEntity = dataEntityList.get(groupPosition);
        if (itemEntity != null) {
            viewHolder.title.setText(itemEntity.getTitle());
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_group_item, parent, false);
            viewHolder = new ChildViewHolder(convertView);
            convertView.setTag(viewHolder);
            dataEntityList.get(groupPosition).getData().get(childPosition).setView(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        ExpandItemEntity itemEntity = dataEntityList.get(groupPosition).getData().get(childPosition);
        if (itemEntity != null) {
            viewHolder.subTitle.setText(itemEntity.getSubTitle());
            viewHolder.progressBar.setProgress(itemEntity.getProgress());
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class GroupViewHolder {
        public TextView title;

        public GroupViewHolder(View viewRoot) {
            title = viewRoot.findViewById(R.id.title);
        }
    }

    public class ChildViewHolder {
        public TextView subTitle;
        public ProgressBar progressBar;

        private ChildViewHolder(View viewRoot) {
            subTitle = viewRoot.findViewById(R.id.subTitle);
            progressBar = viewRoot.findViewById(R.id.progress);
        }
    }

    public ChildViewHolder getChildViewHolder(int groupPosition, int childPosition) {
        return (ChildViewHolder) dataEntityList.get(groupPosition).getData().get(childPosition).getView();
    }
}
